/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "cells.h"
#include <QtWidgets>
#include <QCursor>
#define IMAGE_USING 0


float Aircraft::Aircraft_size_R =80.0;//图标大小
float Aircraft::GoldenRatio = (sin(pi/4)*2-1)/2;
int Aircraft::track_id_max = 1000;


//!maptile
MapTile::MapTile(int tile_x, int tile_y, int zoom, QGraphicsItem *parent, MapType maptype, uint32_t tile_size_width, uint32_t tile_size_height)
{
    this->setParentItem(parent);
    this->m_tile_x = tile_x;
    this->m_tile_y = tile_y;
    this->m_zoom = zoom;
    this->m_MapType = maptype;
    this->m_tile_size_width = tile_size_width;
    this->m_tile_size_height = tile_size_height;

    char buffer[1024];
    switch (m_MapType)
    {
        case StreetMap:
        {
            sprintf(buffer, "../tiles_layer17/lyrs=m@258000000&hl=zh-CN&gl=CN&src=app&x=%d&y=%d&z=%d", m_tile_x, m_tile_y, m_zoom);
            break;
        }
        case SatelliteMap:
        {
            sprintf(buffer, "../tiles_layer17_s/lyrs=m@258000000&hl=zh-CN&gl=CN&src=app&x=%d&y=%d&z=%d", m_tile_x, m_tile_y, m_zoom);
            break;
        }
        default:
            sprintf(buffer, "../tiles_layer17/lyrs=m@258000000&hl=zh-CN&gl=CN&src=app&x=%d&y=%d&z=%d", m_tile_x, m_tile_y, m_zoom);
    }
    img = new QImage(buffer);
    setAcceptDrops(true);
    setAcceptHoverEvents(true);
}

MapTile::~MapTile()
{
    while(img!=NULL)
        delete img;
}

QRectF MapTile::boundingRect() const
{
    return QRectF(0, 0, m_tile_size_width, m_tile_size_height);
}

//以本地坐标中的 QPainterPath 形式返回item的形状。形状可用于碰撞检测，命中测试以及 QGraphicsScene::items() 函数
QPainterPath MapTile::shape() const
{
    QPainterPath path;
    path.addRect(0, 0, m_tile_size_width, m_tile_size_height);
    return path;
}

void MapTile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget);
    Q_UNUSED(option);
    painter->drawImage(QRectF(0, 0, m_tile_size_width, m_tile_size_height), *img);
}

void MapTile::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mousePressEvent(event);
    update();
}

void MapTile::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
#if 0
    if (event->modifiers() & Qt::ShiftModifier) {
        stuff << event->pos();

        return;
    }
#endif
    QGraphicsItem::mouseMoveEvent(event);
    update();
}

void MapTile::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseReleaseEvent(event);
    update();
}


//!aircraft icon
Aircraft::Aircraft(uint32_t TargetID, AircraftParam& TargetInfo, uint8_t TargetMark):
     m_AircraftParam(TargetInfo), m_TargetMark(TargetMark)
{
    m_TargetID = TargetID%track_id_max;
    m_R = Aircraft_size_R;
    m_r = m_R*GoldenRatio;
    m_color = Qt::red;
    ispressd = false;
    label = new QGraphicsTextItem();
    label->setZValue(Aircraft_Z);
    info = "(R,A) = (" + QString::number(TargetInfo.distance) + "," + QString::number(TargetInfo.angle) + ")";

    label->setPlainText(info);
    label->setPos(m_r, m_r + sqrt((m_R+m_r)*(m_R+m_r)-m_R*m_R));
    label->setVisible(ispressd);

    setZValue(Aircraft_Z+1);
    setAcceptDrops(true);
    setAcceptHoverEvents(true);
    setAcceptedMouseButtons(Qt::LeftButton);
    setFlag(QGraphicsItem::ItemIsSelectable);
}

Aircraft::Aircraft(uint32_t TargetID, float distance, float angle, float speed, uint8_t TargetMark):
    m_TargetID(TargetID),m_TargetMark(TargetMark)
{
#if IMAGE_USING
    img = new QImage(":/target.png");
#endif
    m_AircraftParam.distance = distance;
    m_AircraftParam.angle = angle;
    m_AircraftParam.speed = speed;
    m_TargetID = TargetID%track_id_max;
    info = "(R,A) = (" + QString::number(m_AircraftParam.distance)
            + "," + QString::number(m_AircraftParam.angle) + ")";
    m_R = Aircraft_size_R;
    m_r = m_R*GoldenRatio;
    ispressd = false;
    m_color = Qt::red;
    label = new QGraphicsTextItem();
    label->setZValue(Aircraft_Z+1);
    label->setPlainText(info);
    label->setPos(m_r, m_r + sqrt((m_R+m_r)*(m_R+m_r)-m_R*m_R));
    setZValue(Aircraft_Z+1);
    setAcceptDrops(true);
    setAcceptHoverEvents(true);
    setAcceptedMouseButtons(Qt::LeftButton);
    setFlag(QGraphicsItem::ItemIsSelectable);
}

Aircraft::~Aircraft()
{
    if(label!=nullptr)
        delete label;
}

QRectF Aircraft::boundingRect() const
{
    #if IMAGE_USING
        return QRectF(0, 0, AIRCRAFT_SIZE*AIRCRAFT_SIZE, AIRCRAFT_SIZE*AIRCRAFT_SIZE);
    #else
        return QRectF(0, 0, 2*m_r + 5*m_r, m_r + sqrt((m_R+m_r)*(m_R+m_r)-m_R*m_R));
    #endif
}

QPainterPath Aircraft::shape() const
{
#if 1
    QPainterPath path;
    path.addRect(0, 0, AIRCRAFT_SIZE*AIRCRAFT_SIZE, AIRCRAFT_SIZE*AIRCRAFT_SIZE);
    return path;
#else
    float R = m_R;
    float r = m_r;
    float theat_sin = R/(R+r);
    float theat_cos = sqrt(1-(theat_sin)*theat_sin);
    float theat = asin(theat_sin)/2/pi*360;

    float circle_w = R+(R+r)*theat_sin-r-2*R+r;
    float circle_h = 0;
    float circle_left_pos_w = 0-2*R+r;
    float circle_left_pos_h = r+(R+r)*theat_cos-R;
    float circle_right_pos_w = 2*R-2*R+r;
    float circle_right_pos_h = r+(R+r)*theat_cos-R;

    QRectF LeftCircle_Rect = {circle_left_pos_w, circle_left_pos_h, 2*R, 2*R};
    QRectF RightCircle_Rect = {circle_right_pos_w, circle_right_pos_h, 2*R, 2*R};
    QRectF UpCircle_Rect = {circle_w, circle_h, 2*r, 2*r};

    QPainterPath ppath;
    ppath.moveTo(2*R+r*theat_sin-2*R+r,r+r*theat_cos);
    ppath.arcTo(UpCircle_Rect, -(180.0-theat*2)/2.0, (360.0-theat*2));
    ppath.moveTo(2*R+r*theat_sin-2*R+r,r+r*theat_cos);
    ppath.arcTo(RightCircle_Rect, (90+theat), (90-theat));
    ppath.arcTo(LeftCircle_Rect, 0, (90-theat));
    return ppath;
#endif
}

void Aircraft::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget);
    Q_UNUSED(option);
#if IMAGE_USING
    painter->drawImage(QRectF(0, 0, AIRCRAFT_SIZE, AIRCRAFT_SIZE), *img);
    painter->setPen(QColor(255, 255, 255));

    QString info ;
    if(m_TargetID<10)
        info = QString("00%1").arg(m_TargetID);
    else if(m_TargetID>99)
        info = QString::number(m_TargetID);
    else
        info = QString("0%1").arg(m_TargetID);
    QFont font;
    font.setFamily("Microsoft YaHei");
    font.setPointSize(6);
    painter->setFont(font);
    painter->drawText(QRectF(78.0/256.0*AIRCRAFT_SIZE, 27.0/256.0*AIRCRAFT_SIZE, 105.0/256*AIRCRAFT_SIZE, 105.0/256*AIRCRAFT_SIZE), Qt::AlignCenter, info);
    info.clear();
#else
    //draw target outline
    painter->setPen(QPen(m_color));
    painter->setBrush(QBrush(m_color));
    painter->drawPath(createPaintPath(m_R, m_r));

    //draw target id text
    painter->setPen(QColor(255, 255, 255));
    painter->setBrush(QBrush(QColor(255, 255, 255)));

    QString info ;
    if(m_TargetID<10)
        info = QString("000%1").arg(m_TargetID);
    else if(m_TargetID>=10 && m_TargetID<100)
        info = QString("00%1").arg(m_TargetID);
    else if(m_TargetID>=100 && m_TargetID<1000)
        info = QString("0%1").arg(m_TargetID);
    else
        info = QString::number(m_TargetID);

    QFont font;
    font.setFamily("Courier");
    font.setPixelSize(10);     //航迹编号--字体大小
    font.setBold(true);
    painter->setFont(font);
    painter->drawText(QRectF(m_r/4, m_r/4, m_r*3/2, m_r*3/2), Qt::AlignCenter, info);
    info.clear();

    //draw target infomation text
    font.setFamily("Courier");
    font.setPixelSize(12);
    font.setBold(true);
    painter->setFont(font);
    painter->setPen(Qt::black);
    label->setVisible(ispressd);
    QRectF MarkedRect = {m_r*3/2, sqrt((m_R+m_r)*(m_R+m_r)-m_R*m_R)- m_r/2, 11*m_r/2, 3*m_r/2};

    if(ispressd)
    {
        QBrush brush(Qt::white);
        painter->setPen(QColor(0, 0, 0));
        painter->setBrush(brush);
        painter->drawRect(MarkedRect);
        painter->drawText(MarkedRect, Qt::AlignLeft, creatTargetInfoText(0x03));
    }
#endif
}

void Aircraft::setColor(QColor color)
{
    m_color = color;
}

void Aircraft::setPositionInfo(AircraftParam &params)
{
    memcpy(&m_AircraftParam, &params, sizeof(AircraftParam));
}

QPainterPath Aircraft::createPaintPath(float R, float r)
{
    float theat_sin = R/(R+r);
    float theat_cos = sqrt(1-(theat_sin)*theat_sin);
    float theat = asin(theat_sin)/2/pi*360;

    float circle_w = R+(R+r)*theat_sin-r-2*R+r;
    float circle_h = 0;
    float circle_left_pos_w = 0-2*R+r;
    float circle_left_pos_h = r+(R+r)*theat_cos-R;
    float circle_right_pos_w = 2*R-2*R+r;
    float circle_right_pos_h = r+(R+r)*theat_cos-R;

    QRectF LeftCircle_Rect = {circle_left_pos_w, circle_left_pos_h, 2*R, 2*R};
    QRectF RightCircle_Rect = {circle_right_pos_w, circle_right_pos_h, 2*R, 2*R};
    QRectF UpCircle_Rect = {circle_w, circle_h, 2*r, 2*r};

    QPainterPath ppath;
    ppath.moveTo(2*R+r*theat_sin-2*R+r,r+r*theat_cos);
    ppath.arcTo(UpCircle_Rect, -(180.0-theat*2)/2.0, (360.0-theat*2));
    ppath.moveTo(2*R+r*theat_sin-2*R+r,r+r*theat_cos);
    ppath.arcTo(RightCircle_Rect, (90+theat), (90-theat));
    ppath.arcTo(LeftCircle_Rect, 0, (90-theat));
    return ppath;
}

QString Aircraft::creatTargetInfoText(uint8_t TargetInfo)
{
    TargetInfo = 0;
#if 0
    QString info;
    if(TargetInfo&0x01)
        info += QString("R = %1; ").arg(m_AircraftParam.distance);
    if(TargetInfo&0x02)
        info += QString("A = %1; ").arg(m_AircraftParam.angle);
    if(TargetInfo&0x04)
        info += QString("V = %1; ").arg(m_AircraftParam.speed);
    return info;
#else
    QString str;
    switch (TargetInfo)
    {
        case 0x03:
            str = QString("(%1,%2)").arg((int)m_AircraftParam.distance).arg((int)(m_AircraftParam.angle));
            break;
         default:
            str = QString("(%1,%2)").arg((int)m_AircraftParam.distance).arg((int)(m_AircraftParam.angle));
    }
    return str;
#endif
}

void Aircraft::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    setCursor(Qt::ArrowCursor);
    QGraphicsItem::mousePressEvent(event);
    update();
}

void Aircraft::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    setCursor(Qt::ArrowCursor);
    QGraphicsItem::mouseMoveEvent(event);
    update();
}

void Aircraft::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    setCursor(Qt::ArrowCursor);
    QGraphicsItem::mouseReleaseEvent(event);
    update();
}

void Aircraft::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    ispressd = !ispressd;
    QGraphicsItem::mouseDoubleClickEvent(event);
    update();
}

void Aircraft::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    setCursor(Qt::ArrowCursor);
    QGraphicsItem::hoverEnterEvent(event);
    update();
}

void Aircraft::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    QGraphicsItem::hoverMoveEvent(event);
    update();
}

void Aircraft::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    QGraphicsItem::hoverLeaveEvent(event);
    update();
}


//!position of radar
LocationIcon::LocationIcon(RadarParamInfoStruct info)
{
    memcpy(&m_radarinfo, &info, sizeof(RadarParamInfoStruct));
    img.load(":/places.png");
    setZValue(Location_Z);
    setAcceptDrops(true);
    setAcceptHoverEvents(true);
}

LocationIcon::LocationIcon(float RadarDetectionDistance, float DirectionAngle, float StartAngle, float EndAngle):
    m_RadarDetectionDistance(RadarDetectionDistance), m_DirectionAngle(DirectionAngle), m_StartAngle(StartAngle), m_EndAngle(EndAngle)
{
    img.load(":/places.png");
    setZValue(Location_Z);
    setAcceptDrops(true);
    setAcceptHoverEvents(true);
}

LocationIcon::~LocationIcon()
{
}

QRectF LocationIcon::boundingRect() const
{
#if 1
    return QRectF(-(m_RadarDetectionDistance + LOCATION_ICON_SIZE),
                  -(m_RadarDetectionDistance + LOCATION_ICON_SIZE),
                  2.0 * (m_RadarDetectionDistance + LOCATION_ICON_SIZE),
                  2.0 * (m_RadarDetectionDistance + LOCATION_ICON_SIZE));
#else
    path.addRect(-( + LOCATION_ICON_SIZE),
                 -( + LOCATION_ICON_SIZE),
                 2.0 * ( + LOCATION_ICON_SIZE),
                 2.0 * ( + LOCATION_ICON_SIZE));
#endif
}

QPainterPath LocationIcon::shape() const
{
    QPainterPath path;
#if 1
    path.addRect(-(m_RadarDetectionDistance + LOCATION_ICON_SIZE),
                 -(m_RadarDetectionDistance + LOCATION_ICON_SIZE),
                 2.0 * (m_RadarDetectionDistance + LOCATION_ICON_SIZE),
                 2.0 * (m_RadarDetectionDistance + LOCATION_ICON_SIZE));
#else
    path.addRect(-( + LOCATION_ICON_SIZE),
                 -( + LOCATION_ICON_SIZE),
                 2.0 * ( + LOCATION_ICON_SIZE),
                 2.0 * ( + LOCATION_ICON_SIZE));
#endif
    return path;
}

void LocationIcon::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
    Q_UNUSED(widget);
    Q_UNUSED(item);

    painter->drawImage(QRectF(0, 0, LOCATION_ICON_SIZE, LOCATION_ICON_SIZE), img);
    painter->setPen(QPen(QColor(0,0,0,128), 1, Qt::SolidLine));
    painter->setBrush(QBrush(QColor(144,144,144,50)));
    QRectF rect_top(-(m_RadarDetectionDistance - (134.0/512.0)*LOCATION_ICON_SIZE),
                    -(m_RadarDetectionDistance - (478.0/512.0)*LOCATION_ICON_SIZE),
                    2*m_RadarDetectionDistance,
                    2*m_RadarDetectionDistance);
    int startAngle = (90 - m_DirectionAngle - m_StartAngle) * 16;
    int spanAngle = (-(m_EndAngle - m_StartAngle)) * 16;
    painter->drawPie(rect_top, startAngle, spanAngle);
}

void LocationIcon::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    //!可以成功捕获鼠标点击事件
    QGraphicsItem::mousePressEvent(event);
    update();
}

void LocationIcon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseMoveEvent(event);
    update();
}

void LocationIcon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseReleaseEvent(event);
    update();
}

void LocationIcon::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    //setCursor(Qt::ArrowCursor);
    QGraphicsItem::hoverEnterEvent(event);
    update();
}

void LocationIcon::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    //setCursor(Qt::ArrowCursor);
    QGraphicsItem::hoverMoveEvent(event);
    update();
}

void LocationIcon::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    //setCursor(Qt::OpenHandCursor);
    QGraphicsItem::hoverLeaveEvent(event);
    update();
}


//! location png
LocationPng::LocationPng()
{
    img.load(":/places.png");
    setZValue(Location_Z);
    setAcceptDrops(true);
    setAcceptHoverEvents(true);
}

LocationPng::~LocationPng()
{

}

QRectF LocationPng::boundingRect() const
{
    return QRectF(-1.0 * LOCATION_ICON_SIZE,-1.0 * LOCATION_ICON_SIZE,
                   2.0 * LOCATION_ICON_SIZE, 2.0 * LOCATION_ICON_SIZE);
}

QPainterPath LocationPng::shape() const
{
    QPainterPath path;
    path.addRect(-1.0 * LOCATION_ICON_SIZE,-1.0 * LOCATION_ICON_SIZE,
                  2.0 * LOCATION_ICON_SIZE,2.0 * LOCATION_ICON_SIZE);
    return path;
}

void LocationPng::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
    Q_UNUSED(widget);
    Q_UNUSED(item);
    painter->drawImage(QRectF(0, 0, LOCATION_ICON_SIZE, LOCATION_ICON_SIZE), img);
}

//!MonitoringAreaItem
MonitoringAreaItem::MonitoringAreaItem(float RadarDetectionDistance, std::vector<dvec2> *path):
    m_RadarDetectionDistance(RadarDetectionDistance)
{
    m_path.assign(path->begin(), path->end());
    setZValue(MonitoringArea_Z);
    setAcceptDrops(true);
    setAcceptHoverEvents(true);
}

MonitoringAreaItem::~MonitoringAreaItem()
{
    m_path.clear();
}

QRectF MonitoringAreaItem::boundingRect() const
{
    return QRectF(-(m_RadarDetectionDistance), -(m_RadarDetectionDistance),
                  2.0*(m_RadarDetectionDistance), 2.0*(m_RadarDetectionDistance));
}

QPainterPath MonitoringAreaItem::shape() const
{
    QPainterPath path;
    QPolygonF polygon;
    for(uint32_t i=0; i<m_path.size(); i++)
        polygon << QPoint(m_path.at(i).x, m_path.at(i).y);
    path.addPolygon(polygon);
    return path;
}

void MonitoringAreaItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
    Q_UNUSED(widget);
    Q_UNUSED(item);

    QPainterPath PainterPath;
    QPolygonF polygon;
    for(uint32_t i=0; i<m_path.size(); i++)
        polygon << QPoint(m_path.at(i).x, m_path.at(i).y);

    PainterPath.addPolygon(polygon);
    painter->setPen(QPen(QColor(0, 0, 255, 150), 1, Qt::SolidLine));
    painter->setBrush(QBrush(QColor(0, 0, 255, 50)));
    painter->drawPath(PainterPath);
}

void MonitoringAreaItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->button() == Qt::RightButton)
    {
        //to clear the monitoring effect in this area
    }
    QGraphicsItem::mousePressEvent(event);
    update();
}

void MonitoringAreaItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseMoveEvent(event);
    update();
}

void MonitoringAreaItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseReleaseEvent(event);
    update();
}

Tracker::Tracker(const uint32_t TargetID, QPointF startPos, QColor color):m_TrackID(TargetID),m_color(color)
{
    m_startPos = {0, 0};
    counter = 0;
    memcpy(&m_Region, &startPos, sizeof(QPointF));
    memcpy(&m_endpos, &m_startPos, sizeof(QPointF));
    AircraftParam params = {0,0,0};
    Tracker_vec.push_back(startPos);
    icon = new Aircraft(TargetID, params, 0);
    icon->setParentItem(this);
    MonitorAreaPen   = QColor(0, 0, 255, 150);
    MonitorAreaBrush = QColor(0, 0, 255, 50);
    AlertAreaPen     = QColor(255, 0, 0, 150);
    AlertAreaBrush   = QColor(255, 0, 0, 50);
    defaultColor     = QColor(0,   0, 0, 255);
    m_color = QColor(Qt::gray);
    m_isColored = false;
    setAcceptDrops(true);
    setAcceptHoverEvents(true);
}

Tracker::~Tracker()
{

}

QRectF Tracker::boundingRect() const
{
    return QRectF(-LOCATION_ICON_SIZE,
                  -LOCATION_ICON_SIZE,
                  LOCATION_ICON_SIZE,
                  LOCATION_ICON_SIZE);
}

QPainterPath Tracker::shape() const
{
#if 1
    QPainterPath path;
    for(uint32_t i=0;  i<Tracker_vec.size(); ++i)
        path.lineTo(Tracker_vec.at(i));
    return path;
#endif
}

void Tracker::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
    Q_UNUSED(painter);
    Q_UNUSED(widget);
    Q_UNUSED(item);
}

void Tracker::Lineto(QPointF pos)
{
    QGraphicsLineItem *line = new QGraphicsLineItem(this);
    if(counter == 0)
    {
        line->setLine(0, 0, pos.x()-m_Region.x(), pos.y()-m_Region.y());
        Tracker_vec.append(pos);
        Lines.append(line);
        counter++;
    }
    else
    {
        memcpy(&m_startPos, &m_endpos, sizeof(QPointF));
        memcpy(&m_endpos, &pos, sizeof(QPointF));
        QPointF pos_start = Tracker_vec.last();
        line->setLine(pos_start.x() - m_Region.x(), pos_start.y()-m_Region.y(), pos.x()-m_Region.x(), pos.y()-m_Region.y());
        Tracker_vec.append(pos);
        Lines.append(line);
   }

    switch (DisplayType) {
        case MonitorArea:
        {
            QPen pen(m_color);
            pen.setWidth(3);
            line->setPen(pen);
            icon->setColor(Qt::red);
            break;
        }
        case AlertArea:
        {
            QPen pen(m_color);
            pen.setWidth(3);
            line->setPen(pen);
            icon->setColor(Qt::red);
            break;
        }
        default:
            QPen pen(Qt::black);
            pen.setWidth(TRACKER_WIDTH);
            line->setPen(pen);
            icon->setColor(Qt::red);
            m_isColored = false;
    }
    DisplayType = Others;
}

void Tracker::setTrackerColor(QColor &color)
{
    m_color = color;
    m_isColored = true;
}

void Tracker::setAreaType(AreaType type)
{
    DisplayType = type;
}

bool Tracker::isColored()
{
    return m_isColored;
}

QColor &Tracker::TrackerColor()
{
    return m_color;
}

PolygonAreaItem::PolygonAreaItem(const uint32_t AreaId, QPointF startPos, AreaType type):
    m_AreaId(AreaId), m_Areatype(type)
{
    memcpy(&m_Region,   &startPos, sizeof(QPointF));
    memcpy(&m_startPos, &startPos, sizeof(QPointF));
    memcpy(&m_endPos,   &startPos, sizeof(QPointF));
    PolygonData.push_back(startPos);

    MonitorAreaPen   = QColor(0, 0, 200, 170);
    MonitorAreaBrush = QColor(0, 0, 255, 100);
    AlertAreaPen     = QColor(255, 0, 0, 150);
    AlertAreaBrush   = QColor(255, 0, 0, 50);
    isFinished = false;
    setAcceptDrops(true);
    setAcceptHoverEvents(true);
}

PolygonAreaItem::~PolygonAreaItem()
{
    PolygonData.clear();
}

QRectF PolygonAreaItem::boundingRect() const
{
    return QRectF(-LOCATION_ICON_SIZE,
                  -LOCATION_ICON_SIZE,
                  LOCATION_ICON_SIZE,
                  LOCATION_ICON_SIZE);
}

QPainterPath PolygonAreaItem::shape() const
{

    QPainterPath path;
    QPolygonF polygon;
    for(int i=0; i<PolygonData.size(); i++)
        polygon << PolygonData.at(i);
    path.addPolygon(polygon);
    return path;
}

void PolygonAreaItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
    /*
    Q_UNUSED(painter);
    Q_UNUSED(widget);
    Q_UNUSED(item);
    */
}

void PolygonAreaItem::Lineto(QPointF pos)
{
    memcpy(&m_startPos, &m_endPos, sizeof(QPointF));
    memcpy(&m_endPos,   &pos,      sizeof(QPointF));
    PolygonData.push_back(pos);
    QGraphicsLineItem *LineItem = new QGraphicsLineItem(this);
    QPen LinePen = LineItem->pen();
    switch(m_Areatype)
    {
    case MonitorArea:
    {
        LinePen.setColor(MonitorAreaPen);
        break;
    }
    case AlertArea:
    {
        LinePen.setColor(AlertAreaPen);
        break;
    }
    default:
        LinePen.setColor(Qt::gray);
        break;
    }
    LinePen.setWidth(2);
    LineItem->setPen(LinePen);
    LineItem->setLine(m_startPos.x()-3, m_startPos.y()-3, m_endPos.x()-3, m_endPos.y()-3);
}

void PolygonAreaItem::PolygonFinished()
{
    isFinished = true;
    QPolygonF polygon;
    if(PolygonData.size()>2)
    {
        for(int i=0; i<PolygonData.size(); i++)
            polygon<<QPointF(PolygonData.at(i));
    }
    QList<QGraphicsItem*> list = childItems();
    while(!list.empty())
    {
        if (list.at(0)!=NULL)
            delete list.at(0);
        list.removeAt(0);
    }
    QGraphicsPolygonItem* areaitem = new QGraphicsPolygonItem(polygon);
    areaitem->setParentItem(this);
    QPen pen;
    pen.setWidth(2);

    switch (m_Areatype) {
        case MonitorArea:
        {
            pen.setColor(MonitorAreaPen);
            areaitem->setBrush(QBrush(MonitorAreaBrush));
            break;
        }
        case AlertArea:
        {
            pen.setColor(AlertAreaPen);
            areaitem->setBrush(QBrush(AlertAreaBrush));
            break;
        }
        default:
            break;
    }
    areaitem->setPen(pen);
}

